package com.rambo.aiharmony;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.ashokvarma.bottomnavigation.ShapeBadgeItem;
import com.ashokvarma.bottomnavigation.TextBadgeItem;
import com.rambo.androidnews.fraction.WanAdMainFraction;
import com.rambo.baselib.BaseFractionAbility;
import com.rambo.baselib.constant.ParamConstant;
import com.rambo.baselib.user.UserFraction;
import com.rambo.baselib.util.DpUtil;
import com.rambo.baselib.util.FractionUtils;
import com.rambo.baselib.util.ToastUtil;
import com.rambo.map.MapFraction;
import com.rambo.weather.WeatherFraction;
import ohos.aafwk.ability.fraction.Fraction;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;

public class MainAbility extends BaseFractionAbility {
    WanAdMainFraction androidFraction;
    WeatherFraction weatherFraction;
    MapFraction mapFraction;
    UserFraction meFraction;
    Fraction currentFraction;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        toolbarHolder.setDefaultTitle("i Harmoney")
                .setVisibility(com.rambo.baselib.ResourceTable.Id_toolbar_iv_back, false)
                .setDefaultRightOneIcon(com.rambo.baselib.ResourceTable.Media_icon_add_city)
                .setDefaultRightOneIconClick(new Component.ClickedListener() {
                    @Override
                    public void onClick(Component component) {
                        if (currentFraction != null) {
                            //添加城市
                            ToastUtil.toast("还未实现添加城市天气的逻辑");
                        }
                    }
                })
                .setVisibility(com.rambo.baselib.ResourceTable.Id_toolbar_iv_right_one, false);
        TextBadgeItem numberBadgeItem =
                new TextBadgeItem()
                        .setBorderWidth(DpUtil.dp2px(getResourceManager(), 1.5f))
                        .setBackgroundColor(0xffff5454)
                        .setText("2")
                        .setHideOnSelect(true);

        ShapeBadgeItem shapeBadgeItem =
                new ShapeBadgeItem()
                        .setShape(ShapeBadgeItem.SHAPE_OVAL)
                        .setShapeColor(0xffff5454)
                        .setOffset(DpUtil.dp2px(getResourceManager(), 20), DpUtil.dp2px(getResourceManager(), -20))
                        .setHideOnSelect(true);

        bottomNavBar.clearAll();
        bottomNavBar.setBarMode(BottomNavigationBar.MODE_DEFAULT);
        bottomNavBar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_DEFAULT);
        bottomNavBar
                .addItem(
                        new BottomNavigationItem(
                                ResourceTable.Media_android, "安卓", getContext())
                                .setActiveColor(0xff177EE6)
                                .setBadgeItem(numberBadgeItem))
                .addItem(
                        new BottomNavigationItem(
                                ResourceTable.Media_weather, "天气", getContext())
                                .setActiveColor(0xff177EE6))
                .addItem(
                        new BottomNavigationItem(
                                ResourceTable.Media_map, "地图", getContext())
                                .setActiveColor(0xff177EE6)
                                .setBadgeItem(shapeBadgeItem))
                .addItem(
                        new BottomNavigationItem(
                                ResourceTable.Media_mine, "我", getContext())
                                .setActiveColor(0xff177EE6))
                .setFirstSelectedPosition(0)
                .setBarMode(BottomNavigationBar.MODE_FIXED)
                .initialise();
        androidFraction = new WanAdMainFraction(this);
        FractionUtils.show(getFractionManager(), com.rambo.baselib.ResourceTable.Id_fraction_container,
                currentFraction, androidFraction);
        currentFraction = androidFraction;
        bottomNavBar.setTabSelectedListener(new BottomNavigationBar.OnTabSelectedListener() {
            @Override
            public void onTabSelected(int i) {
                Fraction fraction;
                switch (i) {
                    case 0:
                        if (androidFraction == null) {
                            androidFraction = new WanAdMainFraction(getContext());
                            fraction = androidFraction;
                        } else {
                            fraction = androidFraction;
                        }
                        break;
                    case 1:
                        if (weatherFraction == null) {
                            weatherFraction = new WeatherFraction(getContext());
                            fraction = weatherFraction;
                        } else {
                            fraction = weatherFraction;
                        }
                        break;
                    case 2:
                        if (mapFraction == null) {
                            mapFraction = new MapFraction(getContext());
                            fraction = mapFraction;
                        } else {
                            fraction = mapFraction;
                        }
                        break;
                    default:
                        if (meFraction == null) {
                            meFraction = new UserFraction(getContext());
                            fraction = meFraction;
                        } else {
                            fraction = meFraction;
                        }

                }
                FractionUtils.show(
                        getFractionManager(),
                        com.rambo.baselib.ResourceTable.Id_fraction_container,
                        currentFraction,
                        fraction);
                currentFraction=fraction;
                showHideMenu();
            }

            @Override
            public void onTabUnselected(int i) {

            }

            @Override
            public void onTabReselected(int i) {

            }
        });
    }

    private void showHideMenu() {
        if (currentFraction instanceof WeatherFraction) {
            toolbarHolder
                    .setVisibility(com.rambo.baselib.ResourceTable.Id_toolbar_iv_right_one, true);
            ((WeatherFraction) currentFraction).verifyLocationPermission();
        } else {
            if (currentFraction instanceof MapFraction) {
                ((MapFraction) currentFraction).verifyLocationPermission();
            }
            toolbarHolder
                    .setVisibility(com.rambo.baselib.ResourceTable.Id_toolbar_iv_right_one, false);
        }
    }

    @Override
    protected int getToolbarLayoutResId() {
        return super.getToolbarLayoutResId();
    }

    @Override
    protected boolean isToolbarVisible() {
        return true;
    }

    @Override
    protected boolean isBottomNavBarVisible() {
        return true;
    }

    @Override
    public void onRequestPermissionsFromUserResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsFromUserResult(requestCode, permissions, grantResults);
        if (grantResults.length > 0) {
            switch (requestCode) {
                case ParamConstant.PERMISSION_LOCATION_CODE:
                    //定位权限授权后，天气点位获取天气信息
                    if (currentFraction != null && currentFraction instanceof WeatherFraction) {
                        ((WeatherFraction) currentFraction).verifyLocationPermission();
                    }
                    if (currentFraction != null && currentFraction instanceof MapFraction) {
                        ((MapFraction) currentFraction).verifyLocationPermission();
                    }
                    break;
            }
        }

    }
}
